using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Colman.Rpg.Properties;

namespace Colman.Rpg.Controls
{
    /// <summary>
    /// The blue menu.
    /// </summary>
    public partial class Menu : Panel
    {
        private Bitmap background = null;

        /// <summary>
        /// Creates a new instance of <see cref="Menu" />.
        /// </summary>
        public Menu()
        {
            this.SetStyle(ControlStyles.ContainerControl, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            InitializeComponent();

            this.BackColor = Color.Transparent;
            this.ForeColor = Color.White;

            this.CreateBackgroundBitmap();
        }

        private void CreateBackgroundBitmap()
        {
            if (this.background != null)
                this.background.Dispose();

            this.background = new Bitmap(this.Width, this.Height);

            using (Graphics g = Graphics.FromImage(this.background))
            {
                // Fill the background
                using (SolidBrush brush = new SolidBrush(Color.FromArgb(21, 41, 156)))
                {
                    g.FillRectangle(brush, Rectangle.Inflate(this.ClientRectangle, -3, -3));
                }

                // Draw corners
                g.DrawImageUnscaled(Resources.MenuTopLeft, 0, 0);
                g.DrawImageUnscaled(Resources.MenuTopRight, this.Width - Resources.MenuTopRight.Width - 1, 0);
                g.DrawImageUnscaled(Resources.MenuBottomLeft, 0, this.Height - Resources.MenuBottomLeft.Height - 1);
                g.DrawImageUnscaled(Resources.MenuBottomRight, this.Width - Resources.MenuBottomRight.Width - 1, this.Height - Resources.MenuBottomRight.Height - 1);

                // Draw lines
                g.DrawImage(Resources.MenuTop, Resources.MenuTopLeft.Width, 0, this.Width - Resources.MenuTopLeft.Width - Resources.MenuTopRight.Width, Resources.MenuTop.Height);
                g.DrawImage(Resources.MenuBottom, Resources.MenuBottomLeft.Width, this.Height - Resources.MenuBottom.Height - 1, this.Width - Resources.MenuBottomLeft.Width - Resources.MenuBottomRight.Width, Resources.MenuBottom.Height);
                g.DrawImage(Resources.MenuLeft, 0, Resources.MenuTopLeft.Width, Resources.MenuLeft.Width, this.Height - Resources.MenuBottomLeft.Height - Resources.MenuTopLeft.Height);
                g.DrawImage(Resources.MenuRight, this.Width - Resources.MenuRight.Width - 1, Resources.MenuTopRight.Width, Resources.MenuRight.Width, this.Height - Resources.MenuBottomLeft.Height - Resources.MenuTopRight.Height);
            }

            this.Invalidate();
        }

        /// <summary>
        /// Raises the <see cref="Control.Resize" /> event.
        /// </summary>
        /// <param name="e">An <see cref="EventArgs" /> that contains the event data.</param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            this.CreateBackgroundBitmap();
        }

        /// <summary>
        /// Raises the <see cref="Control.HandleDestroyed" /> event.
        /// </summary>
        /// <param name="e">An <see cref="EventArgs" /> that contains the event data.</param>
        protected override void OnHandleDestroyed(EventArgs e)
        {
            base.OnHandleDestroyed(e);

            if (this.background != null)
            {
                this.background.Dispose();
                this.background = null;
            }
        }

        /// <summary>
        /// Paints the background of the control.
        /// </summary>
        /// <param name="e">A <see cref="PaintEventArgs"/> that contains the event data.</param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            if (this.background != null)
            {
                e.Graphics.DrawImageUnscaled(this.background, 0, 0);
            }
        }
    }
}
